package com.mmdccj.yugu.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.mmdccj.yugu.common.Result;
import com.mmdccj.yugu.dto.MusicListDto;
import com.mmdccj.yugu.dto.SearchMusicDto;
import com.mmdccj.yugu.dto.UserDto;
import com.mmdccj.yugu.entity.MusicList;
import com.mmdccj.yugu.entity.User;
import com.mmdccj.yugu.service.SearchService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @Author: star_
 * @date: 2023-12-09 16:19
 */
@Slf4j
@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private SearchService searchService;

    /**
     * 检查函数
     *
     * @param keyword     关键词
     * @param currentPage 当前页
     * @param pageSize    页面大小
     * @return
     */
    private Result<String> check(String keyword, Integer currentPage, Integer pageSize) {
        if (keyword == null || keyword.equals("")) {
            return Result.error("关键字不能为空");
        }
        if (currentPage == null || pageSize == null) {
            return Result.error("分页信息不能为空");
        }
        return Result.success("成功");
    }

    /**
     * 搜索单曲
     *
     * @param keyword     关键词
     * @param currentPage 当前页
     * @param pageSize    页面大小
     * @return Result<SearchMusicDto>
     */
    @GetMapping("/music")
    public Result<SearchMusicDto> searchMusic(String keyword, Integer currentPage, Integer pageSize) {
        // 解析参数
        keyword = URLDecoder.decode(keyword, StandardCharsets.UTF_8);
        log.info("解析的参数: {}", keyword);
        // 检查错误
        Result<String> check = check(keyword, currentPage, pageSize);

        if (check.getCode() == 1) {
            return Result.success("成功", searchService.searchMusic(keyword, currentPage, pageSize));
        } else {
            return Result.error(check.getMsg());
        }
    }

    /**
     * 搜索歌手
     *
     * @param keyword     关键词
     * @param currentPage 当前页
     * @param pageSize    页面大小
     * @return Result<PageInfo < User>>
     */
    @GetMapping("/singer")
    public Result<PageInfo<User>> searchSinger(String keyword, Integer currentPage, Integer pageSize) {
        //解析参数
        keyword = URLDecoder.decode(keyword, StandardCharsets.UTF_8);
        log.info("解析的参数: {}", keyword);
        // 检查错误
        Result<String> check = check(keyword, currentPage, pageSize);

        if (check.getCode() == 1) {
            return Result.success("成功", searchService.searchSinger(keyword, currentPage, pageSize));
        } else {
            return Result.error(check.getMsg());
        }
    }

    /**
     * 搜索歌单
     *
     * @param keyword     关键词
     * @param currentPage 当前页
     * @param pageSize    页面大小
     * @return Result<PageInfo < MusicListDto>>
     */
    @GetMapping("/musicList")
    public Result<PageInfo<MusicListDto>> musicList(String keyword, Integer currentPage, Integer pageSize) {
        //解析参数
        keyword = URLDecoder.decode(keyword, StandardCharsets.UTF_8);
        log.info("解析的参数: {}", keyword);
        // 检查错误
        Result<String> check = check(keyword, currentPage, pageSize);
        if (check.getCode() == 1) {
            return Result.success("成功", searchService.searchMusicList(keyword, currentPage, pageSize));
        } else {
            return Result.error(check.getMsg());
        }
    }

    @GetMapping("/CD")
    public Result<PageInfo<MusicListDto>> CD(String keyword, Integer currentPage, Integer pageSize) {
        //解析参数
        keyword = URLDecoder.decode(keyword, StandardCharsets.UTF_8);
        log.info("解析的参数: {}", keyword);
        // 检查错误
        Result<String> check = check(keyword, currentPage, pageSize);
        if (check.getCode() == 1) {
            return Result.success("成功", searchService.searchCD(keyword, currentPage, pageSize));
        } else {
            return Result.error(check.getMsg());
        }
    }


    /**
     * 搜索用户
     *
     * @param keyword     关键词
     * @param currentPage 当前页
     * @param pageSize    页面大小
     * @param session     会话session
     * @return Result<PageInfo < UserDto>>
     */
    @GetMapping("/user")
    public Result<PageInfo<UserDto>> user(String keyword, Integer currentPage, Integer pageSize, HttpSession session) {
        //解析参数
        keyword = URLDecoder.decode(keyword, StandardCharsets.UTF_8);
        log.info("解析的参数: {}", keyword);
        // 检查错误
        Result<String> check = check(keyword, currentPage, pageSize);
        if (check.getCode() == 1) {
            String userId = (String) session.getAttribute("user");
            if (userId == null) return Result.error("未登录");
            return Result.success("成功", searchService.searchUser(keyword, currentPage, pageSize, userId));
        } else {
            return Result.error(check.getMsg());
        }
    }

}
